$().ready(function () {
    validateRule();
    var _html = '<a target="_blank" href="' + $('#topicImgUrl').val() + '">\n' +
        '<img alt="image" class="feed-photo" src="' + $('#topicImgUrl').val() + '"></a>';
    $('#topicImgDiv').html(_html);
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/category/update",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true,
                maxlength: 5
            }
        },
        messages: {
            name: {
                required: icon + "请输入名字",
                maxlength: $.validator.format("分类名称不能大于{0}个字符")
            }
        }
    })
}

layui.use('upload', function () {
    var upload = layui.upload;
    //执行实例
    var uploadInst = upload.render({
        elem: '#topicImgUpload', //绑定元素
        url: '/image/upload',
        size: 1000,
        accept: 'file',
        done: function (r) {
            layer.msg(r.msg);
            $('#topicImgDiv').empty();
            $("#topicImgId").val(r.imgId);
            var _html = '<a target="_blank" href="' + r.fileName + '">\n' +
                '<img alt="image" class="feed-photo" src="' + r.fileName + '"></a>';
            $('#topicImgDiv').html(_html);
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});